import pyglet
import random

window = pyglet.window.Window(640,480)

class starfield(object):
    def __init__(self, pos , size, max, speed, color):
        self.stars = []
        self.pos = pos
        self.size = size
        self.max = max
        self.speed = speed
        self.color = color

        for loop in range(0, max):
            star = [random.randrange(0, size[0] - 1),
                    random.randrange(0, size[1] - 1)]
            self.stars.append(star);
    def draw(self):
        p = []
        c = []
        for loop in range(0, self.max):
            point = (self.pos[0] + self.stars[loop][0], self.pos[1] + self.stars[loop][1] )
            p.extend(point)
            c.extend(self.color)

        batch = pyglet.graphics.Batch()
        
        vertex_list = batch.add(len(p)/2, pyglet.gl.GL_POINTS, None,
            ('v2i', tuple(p)),
            ('c3B', tuple(c)))
        batch.draw()
            
    def update(self, ts):
        for loop in range(0, self.max):
            self.stars[loop][0] += self.speed
            if self.stars[loop][0]>self.size[0]:
                self.stars[loop][0] = 0

# Set up the star fields.
s = starfield( (10,10), (640,480), 30, 1, (175,175,175))
s1 = starfield( (10,10), (640,480), 20, 2, (215,215,215))
s2 = starfield( (10,10), (640,480), 10, 3, (255,255,255))

pyglet.clock.schedule_interval(s.update,  1/120.0)
pyglet.clock.schedule_interval(s1.update,  1/120.0)
pyglet.clock.schedule_interval(s2.update,  1/120.0)

@window.event
def on_draw():
    window.clear()
    s.draw()
    s1.draw()
    s2.draw()

pyglet.app.run()
